package cn.dmrliu.edu.publish.controller;

import cn.dmrliu.edu.publish.bean.ToolExamCourseStats;
import cn.dmrliu.edu.publish.bean.ToolExamPaperStats;
import cn.dmrliu.edu.publish.bean.ToolExamQuestionStats;
import cn.dmrliu.edu.publish.bean.ToolExamScoreStats;
import cn.dmrliu.edu.publish.service.ToolExamService;
import cn.dmrliu.edu.publish.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/edu/realtime/exam")
public class ExamController {

    @Autowired
    ToolExamService toolExamService;

    @RequestMapping("/paper")
    public String getPaperStats(@RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateUtil.now();
        }
        List<ToolExamPaperStats> list = toolExamService.getPaperStats(date);

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"试卷id\",\n" +
                "        \"id\": \"paperId\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"试卷名称\",\n" +
                "        \"id\": \"paperName\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"考试人数\",\n" +
                "        \"id\": \"userCt\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"平均分\",\n" +
                "        \"id\": \"avgScore\",\n" +
                "        \"accuracy\": 2\n" +
                "      },\n" +
                "      \n" +
                "      {\n" +
                "        \"name\": \"平均时长\",\n" +
                "        \"id\": \"avgDur\",\n" +
                "        \"accuracy\": 2\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": [ " + StringUtils.join(list, ",") + "]\n" +
                "  }\n" +
                "}";

    }

    @RequestMapping("/course")
    public String getCourseStats(@RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateUtil.now();
        }
        List<ToolExamCourseStats> list = toolExamService.getCourseStats(date);

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"课程id\",\n" +
                "        \"id\": \"courseId\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"课程名称\",\n" +
                "        \"id\": \"courseName\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"考试人数\",\n" +
                "        \"id\": \"userCt\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"平均分\",\n" +
                "        \"id\": \"avgScore\",\n" +
                "        \"accuracy\": 2\n" +
                "      },\n" +
                "      \n" +
                "      {\n" +
                "        \"name\": \"平均时长\",\n" +
                "        \"id\": \"avgDur\",\n" +
                "        \"accuracy\": 2\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": [ " + StringUtils.join(list, ",") + "]\n" +
                "  }\n" +
                "}";
    }

    @RequestMapping("/score")
    public String getScoreStats(@RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateUtil.now();
        }
        List<ToolExamScoreStats> list = toolExamService.getScoreStats(date);

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": [" + StringUtils.join(list, ",") + "]\n " +
                "}";
    }

    @RequestMapping("/question")
    public String getQuestionStats(@RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateUtil.now();
        }
        List<ToolExamQuestionStats> list = toolExamService.getQuestionStats(date);

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"题目id\",\n" +
                "        \"id\": \"questionId\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"正确答题次数\",\n" +
                "        \"id\": \"currentCt\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"答题次数\",\n" +
                "        \"id\": \"answerCt\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"正确率\",\n" +
                "        \"id\": \"currentRate\",\n" +
                "        \"accuracy\": 2\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"正确答题独立用户数\",\n" +
                "        \"id\": \"currentUvCt\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"答题独立用户数\",\n" +
                "        \"id\": \"answerUvCt\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"正确答题用户占比\",\n" +
                "        \"id\": \"currentUvRate\",\n" +
                "        \"accuracy\": 2\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": [" + StringUtils.join(list, ",") + "]\n" +
                "  }\n" +
                "}";
    }
}
